/**
 * created by 小卷毛, 2020-07-03 003
 * Copyright (c) 2020, 416143467@qq.com All Rights Reserved.
 * #                   *********                            #
 * #                  ************                          #
 * #                  *************                         #
 * #                 **  ***********                        #
 * #                ***  ****** *****                       #
 * #                *** *******   ****                      #
 * #               ***  ********** ****                     #
 * #              ****  *********** ****                    #
 * #            *****   ***********  *****                  #
 * #           ******   *** ********   *****                #
 * #           *****   ***   ********   ******              #
 * #          ******   ***  ***********   ******            #
 * #         ******   **** **************  ******           #
 * #        *******  ********************* *******          #
 * #        *******  ******************************         #
 * #       *******  ****** ***************** *******        #
 * #       *******  ****** ****** *********   ******        #
 * #       *******    **  ******   ******     ******        #
 * #       *******        ******    *****     *****         #
 * #        ******        *****     *****     ****          #
 * #         *****        ****      *****     ***           #
 * #          *****       ***        ***      *             #
 * #            **       ****        ****                   #
 */
package com.zhiyou.aifeng.viewmodel

import android.content.Context
import android.view.View
import android.widget.ImageView
import android.widget.PopupWindow
import android.widget.TextView
import androidx.lifecycle.LifecycleOwner
import androidx.recyclerview.widget.RecyclerView
import com.zhiyou.aifeng.common.base.find
import com.zhiyou.aifeng.common.base.getColorEx
import com.zhiyou.aifeng.common.base.inflate
import com.zhiyou.aifeng.common.view.DropPopupWindow
import com.zhiyou.aifeng.common_ui.base.loadLinear
import com.zhiyou.aifeng.common_ui.utils.applyCompletableSchedulers
import com.zhiyou.aifeng.common_ui.viewmodel.BaseViewModel
import com.zhiyou.aifeng.model.ResultData
import com.zhiyou.aifeng.smart_machine.R
import io.reactivex.Completable
import net.idik.lib.slimadapter.SlimAdapter
import java.util.concurrent.TimeUnit

/**
 * 项目名称：Smart_Machine
 * 创建人：小卷毛
 * 创建时间：2020-07-03 17:53
 */
open class DropViewModel: BaseViewModel() {

    private var dropFilter: PopupWindow? = null

    fun delay(
        time: Long = 300,
        onResult: () -> Unit = {}
    ) =
        Completable.timer(time, TimeUnit.MILLISECONDS)
            .compose(applyCompletableSchedulers())
            .subscribe { onResult() }

    fun showDropFilter(
        context: Context,
        hint: TextView,
        anchor: ImageView,
        bottom: View,
        list: List<ResultData>,
        listener: (ResultData) -> Unit
    ) {
        dropFilter = object : DropPopupWindow(
            view = context.inflate(R.layout.pop_layout_filter),
            indicator = anchor
        ) {
            override fun afterInitView(view: View) {
                val recyclerView = view.find<RecyclerView>(R.id.pop_container)
                recyclerView.apply {
                    loadLinear(context)
                    adapter = SlimAdapter.create()
                        .register<ResultData>(R.layout.item_pop_list) { data, injector ->

                            val index = list.indexOf(data)
                            val isLast = index == list.size - 1
                            //解构声明
                            val (name, _, _, check) = data

                            injector.text(R.id.item_pop_title, name)
                                .textColor(
                                    R.id.item_pop_title,
                                    if (check) context.getColorEx(R.color.colorAccent)
                                    else context.getColorEx(R.color.black)
                                )
                                .visibility(
                                    R.id.item_pop_divider1,
                                    if (isLast) View.GONE else View.VISIBLE
                                )
                                .visibility(
                                    R.id.item_pop_divider2,
                                    if (!isLast) View.GONE else View.VISIBLE
                                )
                                .clicked(R.id.item_pop) {
                                    if (!data.isChecked) {
                                        hint.text = name
                                        list.filter { it.isChecked }.forEach { it.isChecked = false }
                                        data.isChecked = true
                                        (adapter as SlimAdapter).notifyDataSetChanged()

                                        if (data.list.isNotEmpty()) {
                                            data.list.forEach { it.isChecked = false }
                                            data.list[0].isChecked = true
                                        }

                                        dropFilter?.dismiss()
                                        delay { listener(data) }
                                    } else {
                                        dropFilter?.dismiss()
                                    }
                                }
                        }
                        .attachTo(this)
                    (adapter as SlimAdapter).updateData(list)
                }
            }
        }
        dropFilter?.showAsDropDown(bottom)
    }

    override fun onDestroy(owner: LifecycleOwner) {
        super.onDestroy(owner)
        dropFilter = null
    }

}